<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        
        .wrap {
            width: 700px;
            min-height: 500px;
            background-color: rgb(99, 213, 245);
            background-repeat: no-repeat;
            background-size: cover;
            margin: 0 auto;
            position: relative;
            overflow: hidden;
        }
        
        .wrap #form {
            width: 460px;
            height: 120px;
            background: #fff;
            border-radius: 12px;
            position: absolute;
            left: 50%;
            top: 116px;
            transform: translateX(-50%);
            display: flex;
            flex-wrap: wrap;
            padding: 26px;
            box-sizing: border-box;
        }
        
        .wrap #form p {
            margin-bottom: 10px;
        }
        
        .wrap #form p input {
            width: 140px;
            height: 28px;
            border-radius: 12px;
            border: solid 1px #19c2ff;
            margin: 0 10px;
            padding: 5px;
            box-sizing: border-box;
        }
        
        .wrap #form p:last-child input {
            width: 70px;
            background: #19c2ff;
            color: #fff;
            outline: none;
        }
        
        .wrap #form p select {
            width: 140px;
            height: 28px;
            border-radius: 12px;
            border: solid 1px #19c2ff;
            margin: 0 5px;
        }
        
        .wrap #table {
            width: 460px;
            border-radius: 12px;
            position: absolute;
            left: 50%;
            top: 275px;
            transform: translateX(-50%);
            box-sizing: border-box;
            overflow: hidden;
        }
        
        .wrap #table thead {
            width: 100%;
            height: 30px;
            line-height: 30px;
            text-align: center;
            border-top-left-radius: 12px;
            border-top-right-radius: 12px;
            color: #fff;
            background: #4cbfe5;
        }
        
        .wrap #table thead a {
            color: #fff;
        }
        
        .wrap #table tbody {
            color: #19c2ff;
        }
        
        .wrap #table tbody a {
            color: #19c2ff;
        }
        
        .wrap #table tbody tr {
            background: #fff;
        }
        
        .wrap #table tbody tr th:nth-child(1) {
            text-align: left;
            padding-left: 20px;
        }
        
        .title {
            width: 100%;
            height: 200px;
            margin-top: 40px;
            text-align: center;
            font-size: 30px;
            color: white;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="title">学生系统</div>
        <div id="form">
            <p>姓名<input id="username" type="text" value="张三123" /></p>
            <p>年龄<input id="age" type="text" value="21" /></p>
            <p>
                性别
                <select id="gender">
            <option>男</option>
            <option>女</option>
            <option>你猜</option>
          </select>
            </p>
            <p>
                <input type="button" value="添加" id="addBtn" />
                <input type="button" value="排序" id="sortBtn" />
            </p>
        </div>
        <table id="table">
            <thead>
                <tr>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>

    <script>
        var iptEle1 = $("#username");
        var iptEle2 = $("#age");
        var selectEle = $("#gender");
        var btnEle1 = $("#addBtn");
        var tbodyEle = $("tbody");
        var btnEle2 = $("#sortBtn");

        var arr = [];
        // 点击添加输入数据
        btnEle1.onclick = function() {
            // 赋值
            var oIpt1 = iptEle1.value;
            var oIpt2 = iptEle2.value;
            var oSelect = selectEle.value;
            var obj = {
                username: oIpt1,
                age: oIpt2,
                gender: oSelect
            };
            arr.push(obj);
            console.log(arr);
            renderDom(arr);

            // 排序按钮的实现
            btnEle2.onclick = function() {
                arr.sort(function(a, b) {
                    return a.age - b.age;
                });
                console.log(arr);
                renderDom(arr);
            };
        };

        // 结构函数
        function renderDom(arr) {
            tbodyEle.innerHTML = "";
            arr.forEach(function(item) {
                // 添加元素
                var trEle = document.createElement("tr");
                trEle.innerHTML = `<td>${item.username}</td><td>${item.age}</td><td>${item.gender}</td>`;
                trEle.style.textAlign = "center";
                tbodyEle.appendChild(trEle);
                var deleteEle = document.createElement("td");
                deleteEle.innerHTML = "删除";
                deleteEle.style.cursor = "pointer";
                trEle.appendChild(deleteEle);

                // 删除功能的实现
                deleteEle.onclick = function() {
                    deleteEle.parentElement.remove();
                };
            });
        }

        function $(ele) {
            return document.querySelector(ele);
        }
    </script>
</body>

</html>